package com.cl.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cl.core.dto.PagingReqDTO;
import com.cl.core.pojo.PlatformUsers;
import com.cl.core.service.impl.UserServiceImpl;
import com.cl.utility.constant.Constant;
import com.cl.utility.constant.Message;
import com.cl.utility.pojo.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author liddhappy
 * @description menu 接口
 * @createTime 2022/12/3 21:23
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UsersController {
    @Autowired
    UserServiceImpl userService;

    @PostMapping("/add")
    @ApiOperation(value = "添加用户")
    public ResultCode<PlatformUsers> addUser(@RequestBody PlatformUsers user) {
        userService.addUser(user);
        return new ResultCode<>(Message.Success(Constant.ADD_SUCCESS),
                userService.selectUser(user.getUserId().toString()));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除用户")
    public ResultCode<PlatformUsers> deleteUser(String userId) {
        PlatformUsers user = userService.selectUser(userId);
        userService.deleteUser(userId);
        return new ResultCode<>(Message.Success(Constant.DELETE_SUCCESS), user);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新用户")
    public ResultCode<PlatformUsers> updateUser(@RequestBody PlatformUsers user) {
        userService.updateUser(user);
        return new ResultCode<>(Message.Success(Constant.UPDATE_SUCCESS),
                userService.selectUser(user.getUserId().toString()));
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取用户")
    public ResultCode<PlatformUsers> getUser(String userId) {
        PlatformUsers user = userService.selectUser(userId);
        return new ResultCode<>(Message.Success(Constant.SELECT_SUCCESS), user);
    }

    /**
     * 分页查找
     *
     * @param reqDTO 分页对象<考试请求>
     * @return 消息对象
     */
    @ApiOperation(value = "分页查找")
    @PostMapping(value = "/paging")
    public ResultCode<IPage<PlatformUsers>> paging(@RequestBody PagingReqDTO<PlatformUsers> reqDTO) {

        //分页查询并转换
        IPage<PlatformUsers> page = userService.paging(reqDTO);

        return new ResultCode<>(Message.Success(Constant.SELECT_SUCCESS), page);
    }

}
